<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/04/24
 * Time: 15:06
 */

namespace HiCommon\Repository;

//use HiCommon\Model\ViewFinanceOrderPay;

use Illuminate\Support\Facades\DB;

class ViewFinanceOrderPayRepository extends BaseRepository
{

    public function base_model()
    {
        return new \HiCommon\Model\ViewFinanceOrderPay();
    }

    /*获取商户下的用户列表 + 统计用户订单数量和交易金额*/
    public function get_user_list_for_shop($arr_search,$n_shop_id){
        $n_pay_ratio = common_pay_ratio();
        $arr_wherein = $arr_search['basic_wherein'];

        $arr_option = [
          'with'=>['user'],
          'where'=>['shop_id'=>$n_shop_id],
          'field'=>['user_id',DB::raw('count(*) as total'),DB::raw('sum(total_fee)*'.$n_pay_ratio.' as pay_price')]
        ];
        $result        = self::base_model()
                             ->basicWherein($arr_wherein)
                             ->where(
                               function($query)
                               use($arr_search) {
                                   $s_keyword = str_trim($arr_search['key_word']);
                                   if (mb_strlen($s_keyword) > 0) {
                                       $query->orWhereHas('user',
                                         function ($query)
                                         use($s_keyword){
                                             $query->where('phone', 'like', '%'.$s_keyword.'%');
                                         })
                                             ->orWhereHas('user',
                                               function ($query)
                                               use($s_keyword){
                                                   $query->where('nickname', 'like', '%'.$s_keyword.'%');
                                               });
                                   }
                               }
                             )
                             ->groupBy('user_id')
                             ->orderby('pay_price',$arr_search['sort_pay_price'])
                             ->orderby('total',$arr_search['sort_total'])
                             ->apipage($arr_option);

        return $result;
    }

    /*获取用户各类订单统计-订单数量和交易金额(按商户)*/
    public function get_user_order_total_for_shop($n_user_id = 0,$n_shop_id = 0,$arr_extend = []){
        $n_pay_ratio = common_pay_ratio();

        $arr_option = [
          'where'=>['user_id'=>$n_user_id,'shop_id'=>$n_shop_id],
          'field'=>['order_type',DB::raw('count(*) as total'),DB::raw('sum(total_fee)*'.$n_pay_ratio.' as pay_price')]
        ];

        $result        = self::base_model()
                             ->where(
                               function($query)
                               use($arr_extend) {
                                   $n_main_type = intval($arr_extend['main_type']);
                                   if ($n_main_type > 0) {
                                       $query->whereHas('order_njyy',
                                         function ($query)
                                         use($n_main_type){
                                             $query->where('main_type', $n_main_type);
                                         });
                                   }
                               }
                             )
                             ->groupBy('order_type')
                             ->commonget($arr_option);
        return $result;
    }

    /*日期区间查询订单数*/
    public function order_count_num($arr_search, $arr_where = [], $arr_wherein = [])
    {
        //只要付款过的订单都算接单
        $arr_date      = format_date($arr_search);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('count(*) as num')];
        $result        = self::base_model()
                             ->where($arr_where)
                             ->basicWherein($arr_wherein)
                             ->where(
                               function ($query)
                               use ($arr_search) {
                                   $n_shop_id = intval($arr_search['shop_id']);
                                   if ($n_shop_id > 0) {
                                       $query->where('shop_id', $n_shop_id);
                                   }
                               }
                             )
                             ->where(
                               function ($query)
                               use ($arr_search) {
                                   $n_main_type = intval($arr_search['main_type']);
                                   if ($n_main_type > 0) {
                                       $query
                                         //                                  ->where('order_type','njyy')
                                         ->whereHas('order_njyy',
                                           function ($query)
                                           use ($n_main_type) {
                                               $query->where('main_type', $n_main_type);
                                           });
                                   }
                               }
                             )
          //          ->where(
          //            function($query)
          //            use($arr_search) {
          //                $n_is_call_helpdriving = intval($arr_search['is_call_helpdriving']);
          //                if ($n_is_call_helpdriving > 0) {
          //                    $query->whereHas('check_vehicle',
          //                      function ($query)
          //                      use($n_is_call_helpdriving){
          //                          $query->where('is_call_helpdriving',$n_is_call_helpdriving);
          //                      });
          //                }
          //            }
          //          )
                             ->whereBetween('created_at', $arr_between)
                             ->groupBy('date')
                             ->get($arr_select);
        return $result;
    }

    /*日期区间查询订单金额*/
    public function order_sum_price($arr_search, $arr_where = [], $arr_wherein = [])
    {
        $n_pay_ratio = common_pay_ratio();

        $arr_date      = format_date($arr_search);
        $arr_between   = $arr_date['date_between'];
        $s_date_format = $arr_date['date_format'];
        $arr_select    = [DB::raw('date_format(created_at,"' . $s_date_format . '") as date'), DB::raw('sum(pay_fee)*'.$n_pay_ratio.' as sum_pay_fee')];
        $res           = self::base_model()
                             ->where($arr_where)
                             ->basicWherein($arr_wherein)
                             ->where(
                               function ($query)
                               use ($arr_search) {
                                   $n_shop_id = intval($arr_search['shop_id']);
                                   if ($n_shop_id > 0) {
                                       $query->where('shop_id', $n_shop_id);
                                   }
                               }
                             )
                             ->whereBetween('created_at', $arr_between)
                             ->groupBy('date')
                             ->orderBy('date', 'desc')
                             ->get($arr_select);
        return $res;
    }


    /***************************************************** 平台 *****************************************************/
    /*平台-订单总数*/
    public function platform_order_count_num($arr_search)
    {
        return self::order_count_num($arr_search);
    }

    /*平台-指定日期总收入*/
    public function platform_order_sum_price($arr_search)
    {
        return self::order_sum_price($arr_search);
    }


    /***************************************************** 检测机构 *****************************************************/
    /*检测机构-订单总数*/
    public function jiance_order_count_num($arr_search, $arr_where = [])
    {
        $arr_wherein = ['order_type' => ['njyy']];
        return self::order_count_num($arr_search, $arr_where);
    }

    /*检测机构-年检预约单-指定日期接单总数*/
    public function jc_njyy_order_count_num($arr_search)
    {
        $arr_where = ['order_type' => 'njyy'];
        return self::order_count_num($arr_search, $arr_where);
    }

    /*检测机构-年检预约单-个人单-指定日期接单总数*/
    public function jc_njyy_geren_order_count_num($arr_search)
    {
        $arr_where               = ['order_type' => 'njyy'];
        $arr_search['main_type'] = 1;
        return self::order_count_num($arr_search, $arr_where);
    }

    /*检测机构-年检预约单-企业单-指定日期接单总数*/
    public function jc_njyy_qiye_order_count_num($arr_search)
    {
        $arr_where               = ['order_type' => 'njyy'];
        $arr_search['main_type'] = 2;
        return self::order_count_num($arr_search, $arr_where);
    }

    /*检测机构-年检预约单-指定日期总收入*/
    public function jc_njyy_order_sum_price($arr_search)
    {
        $arr_where = ['order_type' => 'njyy'];
        return self::order_sum_price($arr_search, $arr_where);
    }


    /***************************************************** 维修店 *****************************************************/
    /*维修店-订单总数*/
    public function weixiu_order_count_num($arr_search, $arr_where = [])
    {
        $arr_wherein = ['order_type' => ['ptshp','wxfw','wxby']];
        return self::order_count_num($arr_search, $arr_where,$arr_wherein);
    }

    /*维修店-普通商品单-指定日期接单总数*/
    public function wx_ptshp_order_count_num($arr_search)
    {
        $arr_where = ['order_type' => 'ptshp'];
        return self::order_count_num($arr_search, $arr_where);
    }

    /*维修店-维修服务单-指定日期接单总数*/
    public function wx_wxfw_order_count_num($arr_search)
    {
        $arr_where = ['order_type' => 'wxfw'];
        return self::order_count_num($arr_search, $arr_where);
    }

    /*维修店-维修保养单-指定日期接单总数*/
    public function wx_wxby_order_count_num($arr_search)
    {
        $arr_where = ['order_type' => 'wxby'];
        return self::order_count_num($arr_search, $arr_where);
    }

    /*维修店-指定日期总收入*/
    public function weixiu_order_sum_price($arr_search, $arr_where = [])
    {
        $arr_wherein = ['order_type' => ['ptshp','wxfw','wxby']];
        return self::order_sum_price($arr_search, $arr_where,$arr_wherein);
    }

    /***************************************************** 道路救援店 *****************************************************/
    /*道路救援店-道路救援单-指定日期接单总数*/
    public function dl_dljy_order_count_num($arr_search)
    {
        $arr_where = ['order_type' => 'dljy'];
        return self::order_count_num($arr_search, $arr_where);
    }

    /*************************************************** 结算 ***************************************************/

    /*账户结算-按商户统计*/
    public function account_settle($arr_where){
        $n_pay_ratio = common_pay_ratio();

        $arr_select    = [DB::raw('sum(total_fee)*'.$n_pay_ratio.' as sum_pay_fee'),DB::raw('sum(total_refund_fee)*'.$n_pay_ratio.' as sum_refund_fee')];
        $result           = self::base_model()
                                ->where($arr_where)
                                ->groupBy('shop_id')
                                ->get($arr_select);
        return $result;
    }

    /*账户结算-按商户统计-不可结算*/
    public function account_settle_can_not($arr_where = []){
        $arr_where['is_settle'] = 2;
        $arr_where['is_finish'] = 2;
        return self::account_settle($arr_where);
    }

    /*账户结算-按商户统计-可结算*/
    public function account_settle_can($arr_where = []){
        $arr_where['is_finish'] = 1;
        $arr_where['is_settle'] = 2;
        return self::account_settle($arr_where);
    }

    /*账户结算-按商户统计-已结算*/
    public function account_settle_already($arr_where = []){
        $arr_where['is_settle'] = 1;
        $arr_where['is_finish'] = 1;
        return self::account_settle($arr_where);
    }

    /*账户结算-数据列表*/
    public function account_settle_list($arr_where = [],$arr_option = []){
        $result           = self::base_model()
                                ->where($arr_where)
                                ->getoption($arr_option)
                                ->get();
        return $result;
    }

    /*账户结算-数据列表-可结算*/
    public function account_settle_list_can($arr_where = [],$arr_option = []){
        $n_pay_ratio = common_pay_ratio();

        $arr_where['is_finish'] = 1;
        $arr_where['is_settle'] = 2;
        ;
        $arr_option['field'] = ['id','order_type',DB::raw('total_fee*'.$n_pay_ratio.' as total_fee'),DB::raw('total_refund_fee*'.$n_pay_ratio.' as total_refund_fee')];
        return self::account_settle_list($arr_where,$arr_option);
    }




}